package com.example.es.controller;

import com.example.es.template.ElasticSearchType;
import com.example.es.template.ElasticsearchTemplate;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.backoff.BackOff;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/es")
public class EsController {

    private final ElasticsearchTemplate elasticsearchTemplate;

    public EsController(ElasticsearchTemplate elasticsearchTemplate) {
        this.elasticsearchTemplate = elasticsearchTemplate;
    }

    @GetMapping("/createIndex")
    public ResponseEntity createIndex() throws IOException {
        final CreateIndexRequest request = new CreateIndexRequest("student");
        // setting 的值
        request.settings(Settings.builder()
                .put("number_of_shards", 1)
                .put("number_of_replicas", 0)
                .put("refresh_interval", "2s")
                .build());

        buildIndexMapping(request);
        elasticsearchTemplate.createIndex(request);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/del")
    public ResponseEntity<Boolean> del() throws IOException {
        return ResponseEntity.ok().body(elasticsearchTemplate.deleteIndex("student"));
    }

    @GetMapping("/add")
    public ResponseEntity<Integer> add() throws IOException {
        final ArrayList<Map<String,Object>> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            final HashMap<String,Object> map = new HashMap<>();
            map.put("name", "name-"+i);
            map.put("desc","desc-"+i);
            map.put("number", i*10);
            map.put("price", i*3.3);
            map.put("title", "title-"+i);
            map.put("province", "湖南");
            map.put("publishTime", new Date());
            list.add(map);
        }
        return ResponseEntity.ok().body(elasticsearchTemplate.batchIndex("student",list).status().getStatus());
    }

    //设置index的mapping
    public void buildIndexMapping(CreateIndexRequest request){
        Map<String, Object> properties = new HashMap<>(8);
        properties.put("name", ElasticSearchType.KEYWORD);
        properties.put("desc", ElasticSearchType.TEXT);
        properties.put("number", ElasticSearchType.INTEGER);
        properties.put("price", ElasticSearchType.DOUBLE);
        properties.put("title", ElasticSearchType.KEYWORD);
        properties.put("province", ElasticSearchType.DOUBLE);
        properties.put("publishTime", ElasticSearchType.DATE);
        final HashMap<String, Object> hashMap = new HashMap<>(4);
        hashMap.put("properties",properties);
        request.mapping(hashMap);
    }
}
